package com.zhkj.txg.module.group.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.group.entity.GroupListResponse
import com.zhkj.txg.module.group.model.GroupManager

class GroupListViewModel(application: Application) : BaseViewModel(application) {
    private val groupManager = GroupManager()
    val groupListLiveData = MutableLiveData<HttpResponse<GroupListResponse>>()

    private var page = 1
    private val numb = 20

    fun getPromotion(
        restart: Boolean = true
    ) {
        val listener = object : ViewModelSubscribeListener<GroupListResponse>(groupListLiveData) {
            override fun onSuccess(resp: GroupListResponse) {
                super.onSuccess(resp)
                resp.hasMore = resp.data?.goods?.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }

        }
        if (restart) page = 1;
        val token = AccountManager.getToken()
        val cart = groupManager.getPromotion(token, page, numb, listener)
        dispose.add(cart)
    }

    fun getGroup(
        restart: Boolean = true
    ) {
        val listener = object : ViewModelSubscribeListener<GroupListResponse>(groupListLiveData) {
            override fun onSuccess(resp: GroupListResponse) {
                super.onSuccess(resp)
                resp.hasMore = resp.data?.goods?.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }

        }
        if (restart) page = 1;
        val token = AccountManager.getToken()
        val cart = groupManager.getGroup(token, page, numb, listener)
        dispose.add(cart)
    }
}